package com.zzxx.mapper;

import com.zzxx.entity.Orders;
import com.zzxx.entity.OrdersExample.Criteria;
import com.zzxx.entity.OrdersExample.Criterion;
import com.zzxx.entity.OrdersExample;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;

public class OrdersSqlProvider {
    public String countByExample(OrdersExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("orders");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String deleteByExample(OrdersExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("orders");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String insertSelective(Orders row) {
        SQL sql = new SQL();
        sql.INSERT_INTO("orders");
        
        if (row.getId() != null) {
            sql.VALUES("id", "#{id,jdbcType=VARCHAR}");
        }
        
        if (row.getBookerid() != null) {
            sql.VALUES("bookerId", "#{bookerid,jdbcType=VARCHAR}");
        }
        
        if (row.getBooktime() != null) {
            sql.VALUES("bookTime", "#{booktime,jdbcType=DATE}");
        }
        
        if (row.getStatus() != null) {
            sql.VALUES("status", "#{status,jdbcType=INTEGER}");
        }
        
        if (row.getChecktype() != null) {
            sql.VALUES("checkType", "#{checktype,jdbcType=INTEGER}");
        }
        
        if (row.getIntroduce() != null) {
            sql.VALUES("introduce", "#{introduce,jdbcType=VARCHAR}");
        }
        
        if (row.getObjtype() != null) {
            sql.VALUES("objType", "#{objtype,jdbcType=INTEGER}");
        }
        
        if (row.getTicketid() != null) {
            sql.VALUES("ticketId", "#{ticketid,jdbcType=VARCHAR}");
        }
        
        if (row.getCouponid() != null) {
            sql.VALUES("couponId", "#{couponid,jdbcType=VARCHAR}");
        }
        
        if (row.getCreatetime() != null) {
            sql.VALUES("createTime", "#{createtime,jdbcType=TIMESTAMP}");
        }
        
        if (row.getManagerid() != null) {
            sql.VALUES("managerId", "#{managerid,jdbcType=VARCHAR}");
        }
        
        if (row.getObjid() != null) {
            sql.VALUES("objId", "#{objid,jdbcType=VARCHAR}");
        }
        
        if (row.getQrsrc() != null) {
            sql.VALUES("qrsrc", "#{qrsrc,jdbcType=VARCHAR}");
        }
        
        if (row.getHexhash() != null) {
            sql.VALUES("hexhash", "#{hexhash,jdbcType=VARCHAR}");
        }
        
        if (row.getContenttype() != null) {
            sql.VALUES("contentType", "#{contenttype,jdbcType=INTEGER}");
        }
        
        if (row.getPrice() != null) {
            sql.VALUES("price", "#{price,jdbcType=DOUBLE}");
        }
        
        return sql.toString();
    }

    public String selectByExample(OrdersExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("bookerId");
        sql.SELECT("bookTime");
        sql.SELECT("status");
        sql.SELECT("checkType");
        sql.SELECT("introduce");
        sql.SELECT("objType");
        sql.SELECT("ticketId");
        sql.SELECT("couponId");
        sql.SELECT("createTime");
        sql.SELECT("managerId");
        sql.SELECT("objId");
        sql.SELECT("qrsrc");
        sql.SELECT("hexhash");
        sql.SELECT("contentType");
        sql.SELECT("price");
        sql.FROM("orders");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        Orders row = (Orders) parameter.get("row");
        OrdersExample example = (OrdersExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("orders");
        
        if (row.getId() != null) {
            sql.SET("id = #{row.id,jdbcType=VARCHAR}");
        }
        
        if (row.getBookerid() != null) {
            sql.SET("bookerId = #{row.bookerid,jdbcType=VARCHAR}");
        }
        
        if (row.getBooktime() != null) {
            sql.SET("bookTime = #{row.booktime,jdbcType=DATE}");
        }
        
        if (row.getStatus() != null) {
            sql.SET("status = #{row.status,jdbcType=INTEGER}");
        }
        
        if (row.getChecktype() != null) {
            sql.SET("checkType = #{row.checktype,jdbcType=INTEGER}");
        }
        
        if (row.getIntroduce() != null) {
            sql.SET("introduce = #{row.introduce,jdbcType=VARCHAR}");
        }
        
        if (row.getObjtype() != null) {
            sql.SET("objType = #{row.objtype,jdbcType=INTEGER}");
        }
        
        if (row.getTicketid() != null) {
            sql.SET("ticketId = #{row.ticketid,jdbcType=VARCHAR}");
        }
        
        if (row.getCouponid() != null) {
            sql.SET("couponId = #{row.couponid,jdbcType=VARCHAR}");
        }
        
        if (row.getCreatetime() != null) {
            sql.SET("createTime = #{row.createtime,jdbcType=TIMESTAMP}");
        }
        
        if (row.getManagerid() != null) {
            sql.SET("managerId = #{row.managerid,jdbcType=VARCHAR}");
        }
        
        if (row.getObjid() != null) {
            sql.SET("objId = #{row.objid,jdbcType=VARCHAR}");
        }
        
        if (row.getQrsrc() != null) {
            sql.SET("qrsrc = #{row.qrsrc,jdbcType=VARCHAR}");
        }
        
        if (row.getHexhash() != null) {
            sql.SET("hexhash = #{row.hexhash,jdbcType=VARCHAR}");
        }
        
        if (row.getContenttype() != null) {
            sql.SET("contentType = #{row.contenttype,jdbcType=INTEGER}");
        }
        
        if (row.getPrice() != null) {
            sql.SET("price = #{row.price,jdbcType=DOUBLE}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("orders");
        
        sql.SET("id = #{row.id,jdbcType=VARCHAR}");
        sql.SET("bookerId = #{row.bookerid,jdbcType=VARCHAR}");
        sql.SET("bookTime = #{row.booktime,jdbcType=DATE}");
        sql.SET("status = #{row.status,jdbcType=INTEGER}");
        sql.SET("checkType = #{row.checktype,jdbcType=INTEGER}");
        sql.SET("introduce = #{row.introduce,jdbcType=VARCHAR}");
        sql.SET("objType = #{row.objtype,jdbcType=INTEGER}");
        sql.SET("ticketId = #{row.ticketid,jdbcType=VARCHAR}");
        sql.SET("couponId = #{row.couponid,jdbcType=VARCHAR}");
        sql.SET("createTime = #{row.createtime,jdbcType=TIMESTAMP}");
        sql.SET("managerId = #{row.managerid,jdbcType=VARCHAR}");
        sql.SET("objId = #{row.objid,jdbcType=VARCHAR}");
        sql.SET("qrsrc = #{row.qrsrc,jdbcType=VARCHAR}");
        sql.SET("hexhash = #{row.hexhash,jdbcType=VARCHAR}");
        sql.SET("contentType = #{row.contenttype,jdbcType=INTEGER}");
        sql.SET("price = #{row.price,jdbcType=DOUBLE}");
        
        OrdersExample example = (OrdersExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(Orders row) {
        SQL sql = new SQL();
        sql.UPDATE("orders");
        
        if (row.getBookerid() != null) {
            sql.SET("bookerId = #{bookerid,jdbcType=VARCHAR}");
        }
        
        if (row.getBooktime() != null) {
            sql.SET("bookTime = #{booktime,jdbcType=DATE}");
        }
        
        if (row.getStatus() != null) {
            sql.SET("status = #{status,jdbcType=INTEGER}");
        }
        
        if (row.getChecktype() != null) {
            sql.SET("checkType = #{checktype,jdbcType=INTEGER}");
        }
        
        if (row.getIntroduce() != null) {
            sql.SET("introduce = #{introduce,jdbcType=VARCHAR}");
        }
        
        if (row.getObjtype() != null) {
            sql.SET("objType = #{objtype,jdbcType=INTEGER}");
        }
        
        if (row.getTicketid() != null) {
            sql.SET("ticketId = #{ticketid,jdbcType=VARCHAR}");
        }
        
        if (row.getCouponid() != null) {
            sql.SET("couponId = #{couponid,jdbcType=VARCHAR}");
        }
        
        if (row.getCreatetime() != null) {
            sql.SET("createTime = #{createtime,jdbcType=TIMESTAMP}");
        }
        
        if (row.getManagerid() != null) {
            sql.SET("managerId = #{managerid,jdbcType=VARCHAR}");
        }
        
        if (row.getObjid() != null) {
            sql.SET("objId = #{objid,jdbcType=VARCHAR}");
        }
        
        if (row.getQrsrc() != null) {
            sql.SET("qrsrc = #{qrsrc,jdbcType=VARCHAR}");
        }
        
        if (row.getHexhash() != null) {
            sql.SET("hexhash = #{hexhash,jdbcType=VARCHAR}");
        }
        
        if (row.getContenttype() != null) {
            sql.SET("contentType = #{contenttype,jdbcType=INTEGER}");
        }
        
        if (row.getPrice() != null) {
            sql.SET("price = #{price,jdbcType=DOUBLE}");
        }
        
        sql.WHERE("id = #{id,jdbcType=VARCHAR}");
        
        return sql.toString();
    }

    protected void applyWhere(SQL sql, OrdersExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}